<?php
	/**
	 * Created by:
	 * User: wangs
	 * Date: 2023/11/21
	 */
	declare (strict_types=1);

	namespace app\service;
	use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
	use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
	use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;

	class ExcelData
	{
		protected array $data=[];//表信息
		protected array $image_data=[];//图片信息
		/**
		 * [
		 *    {"商品id":1,'佣金率': '25%', '产品名': '魔术灵洁厕剂'}
		 *    {"商品id:2,'佣金率': '30%', '产品名': '油污净'}
         * ]
		 */
		protected int $highestColumnIndex;//最大列数
		protected int $highestRow;//最大行数
		protected int $excel_header_int;//表头所在位置
	    protected int 	$compressionQuality = 75;


		protected int $shop_id=1257894;//店铺id

		protected Worksheet $worksheet;


		public function setWorksheet($worksheet)
		{
			$this->worksheet = $worksheet;
		}

		public function setExcelHeaderInt($excel_header_int)
		{

			$this->excel_header_int = (int)$excel_header_int;
		}

		public function run()
		{
			$this->init();//初始化
			$this->getColumData();//获取表格数据
			$this->dataCompletion();//数据补全
			$this->getExcelImag();//获取表格图片
			$this->dataBindImag();//数据绑定图片
			halt($this->image_data);
		}

		/**
		 * 获取最大列 最大行数量
		 * @return void
		 */
		protected function init()
		{
			$this->highestRow = $this->worksheet->getHighestDataRow(); // 获取表格的行数
			$highestColumn = $this->worksheet->getHighestDataColumn(); //获取表头的A B C 列坐标
			$this->highestColumnIndex =Coordinate::columnIndexFromString($highestColumn); //获取列数量
		}

		/**
		 * 按照行获取数据，排除标题
		 * @return void
		 */
		protected function getColumData()
		{
			$data=[];
			for ($row = $this->excel_header_int + 2; $row <= $this->highestRow; ++$row) {
				for ($col = 1; $col <= $this->highestColumnIndex; ++$col) {
					$value = $this->worksheet->getCell([$col, $row])->getValue();
					$data[$row][$col] = $value;
				}
			}
			$this->data=$data;
		}

		protected function getExcelImag(){
			$i=0;
			$drawings=$this->worksheet->getDrawingCollection();
			foreach ($drawings as $drawing) {

				$coordinates=$drawing->getCoordinates();//开始位置
				list($startColumn, $startRow) = Coordinate::coordinateFromString($coordinates);//拆分成 ['A','4'];
				$coordinates2=$drawing->getCoordinates2();//结束位置
				list($startColumn2, $startRow2) = Coordinate::coordinateFromString($coordinates2);//拆分成 ['A','4'];

				$path=$drawing->getPath();//图片地址
				if ($path) {
						// Check if the source is a URL or a file path
						if ($drawing->getIsURL()) {//是url还是文件
							$imageContents = file_get_contents($drawing->getPath());
							$filePath = tempnam(sys_get_temp_dir(), 'Drawing');
							file_put_contents($filePath , $imageContents);
							$mimeType = mime_content_type($filePath);
							// You could use the below to find the extension from mime type.
							// https://gist.github.com/alexcorvi/df8faecb59e86bee93411f6a7967df2c#gistcomment-2722664
							$extension = mime2ext($mimeType);
							unlink($filePath);
						}
						else {
							$zipReader = fopen($drawing->getPath(),'r');
							$imageContents = '';
							while (!feof($zipReader)) {
								$imageContents .= fread($zipReader,1024);
							}
							fclose($zipReader);
							$extension = $drawing->getExtension();
						}
					}
				$myFileName = '00_Image_'.$coordinates.'-'.$coordinates2.'.'.$extension;
				$this->image_data[$i]['Row']=[$startRow,$startRow2];
				//$imag_url=$this->inputImage($myFileName,$imageContents);
				$this->image_data[$i]['path']=$myFileName;
				$i++;
			}
		}

		protected function inputImage($file_name,$file){
			$obj=app("app\\service\\InputOss");
			$obj->setContent($file);
			$obj->createObject($file_name,$this->shop_id);
			return $obj->getUrl();
		}

		//处理关于多sku的数据确实问题
		public function dataCompletion(){
			foreach ($this->data as $key=>$datum){

			}
		}





		//数据绑定图片
		protected function dataBindImag(){
			foreach ($this->data as $key=>&$datum){
				foreach($this->image_data as $image_datum){
					$row=$image_datum['Row'];
					$path=$image_datum['path'];
					if(in_array($key,$row)){
						$datum[2][]=$path;
					}
				}
			}
			halt($this->data);
		}
	}